<?php
$_SESSION['steps'][$_GET['action']] = false;
if($_SESSION['index']=='')
{
	displayError('Une erreur interne s\'est produit (perte du fichier index.xml)');
}
if(isset($_GET['post']) && $_GET['post']==1)
{
	$setup = new SimpleXMLElement($_SESSION['index']);
	$file_sql = $setup->screens[0]->createtable[0]['path'];
	if($file_sql=='')
	{
		$error = 'Erreur lors de la lecture du fichier xml';
?>
document.getElementById('install_content_data').innerHTML = '<span class="install_message_error"><?php echo strToJs($error)?></span>';
document.getElementById('install_next').disabled=true;
<?php
		die();		
	}
	
	include_once($temp_path.'/setup_archive.php5');
	
	$tgz = new gzip_file($_SESSION['pack_path']);
	$tgz->set_options(array('basedir' => $temp_path, 'overwrite' => 1));

	list($errors, $n_succes) = $tgz->extract_files(array(array('', $file_sql)));
	
	$_SESSION['extracted'] = array_merge($_SESSION['extracted'], $n_succes);
	
	if(count($errors)>0)
	{
		$message = 'Erreur lors de l\'extraction de l\'archive<br><br>'.implode('<br>', $errors);
		displayButtons();
?>
document.getElementById('install_content_data').innerHTML = '<span class="install_message_error"><?php echo strToJs($message)?></span>';
document.getElementById('install_next').disabled=true;
<?php
		die();
	}
	
	$sqls = file_get_contents($temp_path.'/'.$file_sql);
	
	
	$link = @mysql_connect($_SESSION['conf_db']['host'], $_SESSION['conf_db']['login'], $_SESSION['conf_db']['password']);
	$error = '';
	if($link===false)
	{
		$error  .= '<span class="install_message_error">Impossible de se connecter au server de données...<br><i>'.mysql_error().'</i></span>';
	}
	else 
	{
		if(!mysql_select_db($_SESSION['conf_db']['database'], $link))
		{
			$error  .= '<span class="install_message_error">Impossible de séléctionner la base...<br><i>'.mysql_error().'</i></span>';
		}
	}
	if($error!='')
	{
?>
document.getElementById('install_content_data').innerHTML = '<span class="install_message_error"><?php echo strToJs($error)?></span>';
document.getElementById('install_next').disabled=true;
<?php
		die();		
	}
	
	$sqls = preg_split('/;[\r\n]+/', $sqls);
	mysql_query('SET FOREIGN_KEY_CHECKS=0');
	
	session_write_close();
	$nb = count($sqls);
	$last_progree= microtime(true);
	foreach ($sqls as $index => $sql)
	{
		if((microtime(true)-$last_progree)*1000>800)
		{
			session_start();
			$_SESSION['progress'] = round(($index*1000)/$nb);
			session_write_close();
			$last_progree= microtime(true);
		}
		
		$sql = trim($sql);
		if($sql=='')continue;
		$sql = str_replace('ttprefix__', $_SESSION['conf_db']['prefix'], $sql);
		mysql_query($sql);
		$err = mysql_error();
		if($err!='')
		{
			$errors[] = $err."<br>".nl2br($sql);
		}
	}
	session_start();
	mysql_query('SET FOREIGN_KEY_CHECKS=1');

	if(count($errors)>0)
	{
		$message = 'Erreur lors de l\'execution des requettes SQL<br><br>'.implode('<hr>', $errors);
		displayButtons();
?>
document.getElementById('install_content_data').innerHTML = '<span class="install_message_error"><?php echo strToJs($message)?></span>';
document.getElementById('install_next').disabled=true;
<?php
		die();
	}
?>
clearTimeout(to)
document.getElementById("div_table").style.width = "100%";
document.getElementById("div_table_label").innerHTML = "100%";
<?php
	$_SESSION['steps'][$_GET['action']] = true;
	displayButtons();
?>
	document.getElementById('install_next').onclick();
<?php
}
else 
{
		$_SESSION['progress'] = 0;
?>
document.getElementById('install_content_intro').innerHTML = 'Veuillez patienter pendant que l\'installeur créé les tables.';
document.getElementById('install_content_data').innerHTML = '<div style="position:absolute;top:5px;left:10px;right:10px;border:1px solid #606060"><div style="background-color:#6999DA;width:1px;height:16px;overflow:hidden" id="div_table">&nbsp;</div><div style="position:absolute;left:0px;top:0px;right:0px;bottom:0px;text-align:center; font-weight:bold;" id="div_table_label">&nbsp;</div></div>'
document.getElementById('install_next').style.display='none'
document.getElementById('install_back').style.display='none'

ajax.sendAsync(page+'?action=copy_table&post=1uid='+new Date().getTime(), function(datas)
{
	eval(datas)
})
ajax.sendAsync(page+'?action=copy_table_info&uid='+new Date().getTime(), function(datas)
{
	evalAjax(datas)
})
<?php
}
?>